package cn.whdreamblog.daoimp;

import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;

import cn.ccit.base.BaseConstants;
import cn.whdreamblog.dao.MessageDAO;
import cn.whdreamblog.entites.BlogArticle;
import cn.whdreamblog.entites.Message;
import cn.whdreamblog.entites.PageBean;
import cn.whdreamblog.util.MyDBUtil;

public class MessageDAOImp extends BaseModelDAOImp implements MessageDAO {
	
	public MessageDAOImp() {
		super();
	}
	
	@Override
	public boolean insertMessage(Message msg) {
		if(msg.getBlogArticleId() != BaseConstants.DEAFAULT_COMMENT_CODE){
			long count = getDataCount(Message.CLASS_NAME,new String[]{"blogArticleId"},
					new String[]{String.valueOf(msg.getBlogArticleId())},true);
			updateData(BlogArticle.CLASS_NAME,"messageCount",
					String.valueOf((count+1)),new String[]{MyDBUtil._id},
					new String[]{String.valueOf(msg.getBlogArticleId())},true);
		}
		return insertData(msg);
	}

	@Override
	public boolean deleteMessage(Message msg) {
		return removeData(msg);
	}

	@Deprecated
	public List<Message> getAllMessages() {
		if(sessionFactory!=null){
			session=sessionFactory.openSession();
			try {
				tx=session.beginTransaction();
				List<Message> list = session.createQuery("from Message")
										.list();
				
				tx.commit();
				if(list!=null&&list.size()>0){
					return list;
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				MyDBUtil.rollBack(tx);
				
			}finally{
				MyDBUtil.closeSession(session, tx);
			}
		}
		return null;
	}
	@Override
	public Message getMessageById(Message msg) {
//		if(sessionFactory!=null){
//			session=sessionFactory.openSession();
//			try {
//				Message temp=null;
//				tx=session.beginTransaction();
//				temp=session.get(Message.class, msg.getId());
//				tx.commit();
//				if(temp!=null){
//					return temp;
//				}
//				
//			} catch (Exception e) {
//				e.printStackTrace();
//				MyDBUtil.rollBack(tx);
//				
//			}finally{
//				MyDBUtil.closeSession(session, tx);
//			}
//		}
//		return null;
		return super.getDataById(msg);
	}

	@Override
	public List<Message> getAllMessages(int start, int limit, int blogId) {
		
		return super.getSomeData(start, limit, 
				Message.CLASS_NAME, 
				new String[]{"blogArticleId"},
				new String[]{String.valueOf(blogId)}, true);
	}
	@Override
	public PageBean getAllMessages2Map(int page, int limit, int blogId) {
		Map map = super.getSomeData2Map(PageBean.countOffset(limit, PageBean.countCurrentPage(page)), limit, 
				Message.CLASS_NAME, 
				new String[]{"blogArticleId"},
				new String[]{String.valueOf(blogId)}, true);
		PageBean pageBean = new PageBean();
		if(map == null){
			return null;
		}
		pageBean = pageBean.setPageData(
				(List)map.get("list"),pageBean,(Long)map.get("count"), limit, page);
		return pageBean;
	}
	public PageBean getAllMessages2Map(int page, int limit) {
		Map map = super.getSomeData2Map(PageBean.countOffset(limit, PageBean.countCurrentPage(page)), limit, 
				Message.CLASS_NAME);
		PageBean pageBean = new PageBean();
		if(map == null){
			return null;
		}
		pageBean = pageBean.setPageData(
				(List)map.get("list"),pageBean,(Long)map.get("count"), limit, page);
		return pageBean;
	}
	
	@Override
	public Message getMessageByAttr(Message msg, String attr) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Message> getAllMessages(int start, int limit) {
		return super.getSomeData(start, limit, 
				Message.CLASS_NAME);
	}
	
	

}
